local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.ModelObject());


local BoxPerfab=CommonOHOH:GetPath_Loop("ReplicatedStorage.Box",game)

local ConnectClass=_G.CommonOHOH.load.Connect();
local HttpService = game:GetService("HttpService")


function gGetBoxObject()
    local model= BoxPerfab:Clone()
    for _,p in pairs( model:GetDescendants()) do
        if p:IsA("BasePart") then
            p.Anchored=true
            p.CanTouch=false
            p.CanCollide=false
            if p.Name=="Instance" then
                p.CanTouch=true
            end
        end
    end
    model.PrimaryPart=model:FindFirstChild("Instance")
    return model
 end

 function CreateGuid()
	return HttpService:GenerateGUID(false)
end


-- 宝箱
function M.new(...)
	return M(...);
end;

function M.ctor(self,itemId)--构造函数
    self.itemId=itemId
    self.DeadEvent=ConnectClass.new() -- 死亡
    self.HurtEvent=ConnectClass.new() -- 受伤
    self.AttackEvent=ConnectClass.new() -- 受伤
    self.Guid=CreateGuid()
    self.Hp=0
    self.Damge=0
	M.super.ctor(self,gGetBoxObject());
    self.attribute.IsCarTouch=true
    -- self:AddPrivateComponentByFolder(script.com)
end;

function M.dtor(self)
    self.DeadEvent:dtor()
    self.HurtEvent:dtor()
    self.AttackEvent:dtor()

	M.super.dtor(self);
    self.Obj:Destroy()
    self.Obj=nil
end;


function M.GetDamge(self)
    return  self.Damge
end

function M.GetHp(self)
    return  self.Hp
end

function M.GetItem(self)
    return  self.itemId,nil
end

--受伤
function M.Hurt(self,damge,attackerList)
    self.Hp=self.Hp-damge
    self.HurtEvent:Action(damge,attackerList)
    if self.Hp<=0 then
        self.Hp=0
        self:Dead()
    end
end


function M.GetGuid(self)
    return self.Guid
end

function M.Installation(self,cf,parent)
    self.main_inst:PivotTo(cf)
    self.main_inst.Parent=parent
end

function M.Dead(self)
   
    self.Obj=self.main_inst
    self:AddProcess(function()
        self.DeadEvent:Action()
        task.wait()
        self:dtor()
    end)
   
end

return M